gsub_file "Gemfile", "https://rubygems.org", "https://gems.ruby-china.org"

# 格式清理
gem 'sanitize'
# 数据库连接
gem 'connection_ninja'
# 基于环境的配置
gem 'figaro'
# 网络请求库
gem 'httparty', '~> 0.14.0'
# 异步队列
gem 'resque', '1.27.2'
# 队列调度
gem 'resque-scheduler', '4.3.0'
# 表单
gem 'formtastic', '~> 3.0'
# 表单样式
gem 'formtastic-bootstrap'
# 分页
gem 'kaminari', '~> 0.17.0'
# 嵌套表单
gem 'nested_form'
# insert values语法
gem 'activerecord-import', '~> 0.4.0'
# 身份认证
gem 'devise'
# 搜索
gem 'elasticsearch-model'
gem 'elasticsearch-rails', '~> 0.1.9'

# web服务器
gem 'puma'

gem_group :development do
  gem 'web-console', '~> 2.0'
  gem 'spring'
  gem 'quiet_assets'
  gem 'better_errors'
  gem 'awesome_print'
end
# 初始化redis
initializer '0_redis.rb', <<-CODE
  config = Rails.application.config_for(:redis)
  $REDIS = Redis.new(host: config[:host], port: config[:port])
CODE

# 创建redis配置文件
file 'config/redis.yml', <<-CODE
  defaults: &defaults
    host: localhost
    port: 6379

  development:
    <<: *defaults

  test:
    <<: *defaults

  production:
    <<: *defaults
CODE

# 初始化elasticsearch
initializer 'elasticsearch.rb', <<-CODE
  config = Rails.application.config_for(:elasticsearch)
  Elasticsearch::Model.client = Elasticsearch::Client.new host: config['host']
CODE

# 创建elasticsearch
file 'config/elasticsearch.yml', <<-CODE
  defaults: &defaults
    host: localhost:9200

  development:
    <<: *defaults

  test:
    <<: *defaults

  production:
    <<: *defaults
CODE

# 初始化分页
initializer 'kaminari_config.rb', <<-CODE
  Kaminari.configure do |config|
    config.default_per_page = 25
    # config.max_per_page = nil
    # config.window = 4
    # config.outer_window = 0
    # config.left = 0
    # config.right = 0
    # config.page_method_name = :page
    # config.param_name = :page
  end
CODE

# 初始化默认时间显示样式
initializer 'rails_defaults.rb', <<-CODE
  # 修改默认时间格式
  Date::DATE_FORMATS[:default] = '%Y-%m-%d'

  # if you want to change the format of Time display then add the line below
  Time::DATE_FORMATS[:default]= '%Y-%m-%d %H:%M:%S'

  # if you want to change the DB date format.
  Time::DATE_FORMATS[:db]= '%Y-%m-%d %H:%M:%S'

  class ActiveSupport::TimeWithZone
      def as_json(options = {})
          strftime('%Y-%m-%d %H:%M:%S')
      end
  end
CODE

# 创建全局环境配置
file 'config/application.yml', <<-CODE
  development:

  production:

CODE

# 创建pids
inside('tmp') do
  run "mkdir pids"
end

environment <<-CODE
  config.active_record.default_timezone = :local
  config.time_zone = 'Beijing'
  config.i18n.default_locale = :zh
  config.active_job.queue_adapter = :resque
CODE

after_bundle do
  # 加载config文件
  run "wget http://git.oschina.net/DeveloperLee/railstemplate/raw/master/config/puma.rb -O config/puma.rb"
  run "wget http://git.oschina.net/DeveloperLee/railstemplate/raw/master/config/puma_production.rb -O config/puma_production.rb"
  # 下载本地化文件
  run "wget http://git.oschina.net/DeveloperLee/railstemplate/raw/master/local/devise.en.yml -O config/locales/devise.en.yml"
  run "wget http://git.oschina.net/DeveloperLee/railstemplate/raw/master/local/devise.zh.yml -O config/locales/devise.zh.yml"
  run "wget http://git.oschina.net/DeveloperLee/railstemplate/raw/master/local/zh.yml -O config/locales/zh.yml"
  # 加载shell
  run "wget http://git.oschina.net/DeveloperLee/railstemplate/raw/master/shell/puma.sh"
  run "wget http://git.oschina.net/DeveloperLee/railstemplate/raw/master/shell/puma_production.sh"
  # 加载rake
  run "wget http://git.oschina.net/DeveloperLee/railstemplate/raw/master/rake/elasticsearch.rake -O lib/tasks/elasticsearch.rake"
  run "wget http://git.oschina.net/DeveloperLee/railstemplate/raw/master/rake/resque.rake -O lib/tasks/resque.rake"
end